import { Op } from 'sequelize';
import sequelize from '../db/index.js';
import user from '../model/user.js';
import follows from '../model/follows.js';

export async function addUser(data) {
  await user.create(data);
  return {
    resCode: 200,
    message: '成功',
  };
}

export async function queryUserByUserName(user_name) {
  return await user.findAll({
    where: {
      user_name,
    },
  });
}

export async function queryUserById(user_id) {
  return await user.findAll({
    attributes: ['user_id', 'user_name', 'user_description', 'user_reputation', 'user_prefer', 'user_permission', 'user_question_count', 'user_avatar'],
    where: {
      user_id,
    },
  });
}

export async function changeUserReputation(user_id, value) {
  return user.increment(
    { user_reputation: value },
    {
      where: {
        user_id,
      },
    },
  );
}

export async function queryPopularUser() {
  return await user.findAll({
    attributes: ['user_id', 'user_name', 'user_reputation', 'user_prefer', 'user_permission', 'user_question_count', 'user_avatar'],
    order: [['user_reputation', 'DESC']],
    limit: 10,
  });
}

export async function changeUserAvatar(user_id, user_avatar) {
  // return await user.update({user_avatar},{
  //   where: {
  //     user_id
  //   }
  // })

  return await sequelize.query(`UPDATE user SET user_avatar = ? WHERE user_id = ?`, {
    replacements: [user_avatar, user_id],
    type: sequelize.QueryTypes.UPDATE,
  });
}

export async function userEdit(userInfo) {
  return await user.update(
    {
      user_name: userInfo.user_name,
      user_description: userInfo.user_description,
      user_prefer: userInfo.user_prefer,
    },
    {
      where: {
        user_id: userInfo.user_id,
      },
    },
  );
}

export async function queryPopularUserByTag(tag) {
  return await user.findAll({
    attributes: ['user_id', 'user_name', 'user_reputation', 'user_prefer', 'user_permission', 'user_question_count', 'user_avatar'],
    where: {
      user_prefer: {
        [Op.like]: `%${tag}%`,
      },
    },
    order: [['user_reputation', 'DESC']],
    limit: 10,
  });
}

export async function userFollow(follower, followed) {
  return await follows.create({
    follower,
    followed,
  });
}

export async function userUnFollow(follower, followed) {
  return await follows.destroy({
    where: {
      follower,
      followed,
    },
  });
}

export async function queryIsFollowed(follower, followed) {
  return await follows.findAll({
    where: {
      follower,
      followed,
    },
  });
}

// export async function queryUserFollowed(follower){
//   return await follows.findAll({
//     where:{
//       follower
//     }
//   })
// }

export async function queryUserFollowed(follower) {
  const follow = await follows.findAll({
    where: {
      follower,
    },
  });
  return await user.findAll({
    attributes: ['user_id', 'user_name', 'user_reputation', 'user_prefer', 'user_permission', 'user_question_count', 'user_avatar'],
    where: {
      user_id: {
        [Op.in]: follow.map((item) => item.followed),
      },
    },
  });
}

// export async function queryUserBeFollowed(followed){
//   return await follows.findAll({
//     where:{
//       followed
//     }
//   })
// }

export async function queryUserBeFollowed(followed) {
  const follow = await follows.findAll({
    where: {
      followed,
    },
  });
  return await user.findAll({
    attributes: ['user_id', 'user_name', 'user_reputation', 'user_prefer', 'user_permission', 'user_question_count', 'user_avatar'],
    where: {
      user_id: {
        [Op.in]: follow.map((item) => item.follower),
      },
    },
  });
}

export async function queryUsersByLevel(level) {
  return await user.findAll({
    where: {
      user_permission: level,
    },
  });
}

export function deleteUserById(user_id) {
  return user.destroy({
    where: {
      user_id,
    },
  });
}
